package com.hxl.tech.gateway.auth.dao;


import com.hxl.tech.gateway.auth.dto.resource.ResourceFindDTO;
import com.hxl.tech.gateway.auth.model.QResource;
import com.hxl.tech.gateway.auth.model.Resource;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class ResourceDao {

    @javax.annotation.Resource
    private JPAQueryFactory jpaQueryFactory;

    private QResource qResource = QResource.resource;

    public Resource read(String merchantCode, String resourceType) {

        JPAQuery<Resource> query = jpaQueryFactory.select(qResource).from(qResource)
                .where(qResource.id.merchantCode.eq(merchantCode).and(qResource.id.resourceType.eq(resourceType)));
        return query.fetchOne();
    }

    public List<Resource> find(ResourceFindDTO findDTO) {

        JPAQuery<Resource> query = jpaQueryFactory.select(qResource).from(qResource);
        if (StringUtils.isNotEmpty(findDTO.getResourceType())) {
            query.where(qResource.id.resourceType.eq(findDTO.getResourceType()));
        }

        return query.fetch();
    }
}
